package org.jivesoftware.smackx;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.Cache;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.packet.DiscoverInfo;
import org.jivesoftware.smackx.packet.DiscoverItems;
import org.jivesoftware.smackx.packet.DiscoverItems.Item;
import org.jivesoftware.smackx.packet.MultipleAddresses;
import org.jivesoftware.smackx.packet.MultipleAddresses.Address;

public class MultipleRecipientManager
{
  private static Cache<String, String> services = new Cache(100, 86400000L);

  public static void send(Connection paramConnection, Packet paramPacket, List<String> paramList1, List<String> paramList2, List<String> paramList3)
    throws XMPPException
  {
    send(paramConnection, paramPacket, paramList1, paramList2, paramList3, null, null, false);
  }

  public static void send(Connection paramConnection, Packet paramPacket, List<String> paramList1, List<String> paramList2, List<String> paramList3, String paramString1, String paramString2, boolean paramBoolean)
    throws XMPPException
  {
    String str = getMultipleRecipienServiceAddress(paramConnection);
    if (str != null)
    {
      sendThroughService(paramConnection, paramPacket, paramList1, paramList2, paramList3, paramString1, paramString2, paramBoolean, str);
    }
    else
    {
      if ((paramBoolean) || ((paramString1 != null) && (paramString1.trim().length() > 0)) || ((paramString2 != null) && (paramString2.trim().length() > 0)))
        throw new XMPPException("Extended Stanza Addressing not supported by server");
      sendToIndividualRecipients(paramConnection, paramPacket, paramList1, paramList2, paramList3);
    }
  }

  public static void reply(Connection paramConnection, Message paramMessage1, Message paramMessage2)
    throws XMPPException
  {
    MultipleRecipientInfo localMultipleRecipientInfo = getMultipleRecipientInfo(paramMessage1);
    if (localMultipleRecipientInfo == null)
      throw new XMPPException("Original message does not contain multiple recipient info");
    if (localMultipleRecipientInfo.shouldNotReply())
      throw new XMPPException("Original message should not be replied");
    if (localMultipleRecipientInfo.getReplyRoom() != null)
      throw new XMPPException("Reply should be sent through a room");
    if (paramMessage1.getThread() != null)
      paramMessage2.setThread(paramMessage1.getThread());
    MultipleAddresses.Address localAddress = localMultipleRecipientInfo.getReplyAddress();
    if ((localAddress != null) && (localAddress.getJid() != null))
    {
      paramMessage2.setTo(localAddress.getJid());
      paramConnection.sendPacket(paramMessage2);
    }
    else
    {
      ArrayList localArrayList1 = new ArrayList();
      ArrayList localArrayList2 = new ArrayList();
      Object localObject = localMultipleRecipientInfo.getTOAddresses().iterator();
      while (((Iterator)localObject).hasNext())
      {
        str = ((MultipleAddresses.Address)((Iterator)localObject).next()).getJid();
        localArrayList1.add(str);
      }
      localObject = localMultipleRecipientInfo.getCCAddresses().iterator();
      while (((Iterator)localObject).hasNext())
      {
        str = ((MultipleAddresses.Address)((Iterator)localObject).next()).getJid();
        localArrayList2.add(str);
      }
      if ((!localArrayList1.contains(paramMessage1.getFrom())) && (!localArrayList2.contains(paramMessage1.getFrom())))
        localArrayList1.add(paramMessage1.getFrom());
      localObject = paramConnection.getUser();
      if ((!localArrayList1.remove(localObject)) && (!localArrayList2.remove(localObject)))
      {
        str = StringUtils.parseBareAddress((String)localObject);
        localArrayList1.remove(str);
        localArrayList2.remove(str);
      }
      String str = getMultipleRecipienServiceAddress(paramConnection);
      if (str != null)
        sendThroughService(paramConnection, paramMessage2, localArrayList1, localArrayList2, null, null, null, false, str);
      else
        sendToIndividualRecipients(paramConnection, paramMessage2, localArrayList1, localArrayList2, null);
    }
  }

  public static MultipleRecipientInfo getMultipleRecipientInfo(Packet paramPacket)
  {
    MultipleAddresses localMultipleAddresses = (MultipleAddresses)paramPacket.getExtension("addresses", "http://jabber.org/protocol/address");
    return localMultipleAddresses == null ? null : new MultipleRecipientInfo(localMultipleAddresses);
  }

  private static void sendToIndividualRecipients(Connection paramConnection, Packet paramPacket, List<String> paramList1, List<String> paramList2, List<String> paramList3)
  {
    Iterator localIterator;
    String str;
    if (paramList1 != null)
    {
      localIterator = paramList1.iterator();
      while (localIterator.hasNext())
      {
        str = (String)localIterator.next();
        paramPacket.setTo(str);
        paramConnection.sendPacket(new PacketCopy(paramPacket.toXML()));
      }
    }
    if (paramList2 != null)
    {
      localIterator = paramList2.iterator();
      while (localIterator.hasNext())
      {
        str = (String)localIterator.next();
        paramPacket.setTo(str);
        paramConnection.sendPacket(new PacketCopy(paramPacket.toXML()));
      }
    }
    if (paramList3 != null)
    {
      localIterator = paramList3.iterator();
      while (localIterator.hasNext())
      {
        str = (String)localIterator.next();
        paramPacket.setTo(str);
        paramConnection.sendPacket(new PacketCopy(paramPacket.toXML()));
      }
    }
  }

  private static void sendThroughService(Connection paramConnection, Packet paramPacket, List<String> paramList1, List<String> paramList2, List<String> paramList3, String paramString1, String paramString2, boolean paramBoolean, String paramString3)
  {
    MultipleAddresses localMultipleAddresses = new MultipleAddresses();
    Iterator localIterator;
    String str;
    if (paramList1 != null)
    {
      localIterator = paramList1.iterator();
      while (localIterator.hasNext())
      {
        str = (String)localIterator.next();
        localMultipleAddresses.addAddress("to", str, null, null, false, null);
      }
    }
    if (paramList2 != null)
    {
      localIterator = paramList2.iterator();
      while (localIterator.hasNext())
      {
        str = (String)localIterator.next();
        localMultipleAddresses.addAddress("cc", str, null, null, false, null);
      }
    }
    if (paramList3 != null)
    {
      localIterator = paramList3.iterator();
      while (localIterator.hasNext())
      {
        str = (String)localIterator.next();
        localMultipleAddresses.addAddress("bcc", str, null, null, false, null);
      }
    }
    if (paramBoolean)
    {
      localMultipleAddresses.setNoReply();
    }
    else
    {
      if ((paramString1 != null) && (paramString1.trim().length() > 0))
        localMultipleAddresses.addAddress("replyto", paramString1, null, null, false, null);
      if ((paramString2 != null) && (paramString2.trim().length() > 0))
        localMultipleAddresses.addAddress("replyroom", paramString2, null, null, false, null);
    }
    paramPacket.setTo(paramString3);
    paramPacket.addExtension(localMultipleAddresses);
    paramConnection.sendPacket(paramPacket);
  }

  private static String getMultipleRecipienServiceAddress(Connection paramConnection)
  {
    String str1 = paramConnection.getServiceName();
    String str2 = (String)services.get(str1);
    if (str2 == null)
      synchronized (services)
      {
        str2 = (String)services.get(str1);
        if (str2 == null)
          try
          {
            DiscoverInfo localDiscoverInfo = ServiceDiscoveryManager.getInstanceFor(paramConnection).discoverInfo(str1);
            if (localDiscoverInfo.containsFeature("http://jabber.org/protocol/address"))
            {
              str2 = str1;
            }
            else
            {
              DiscoverItems localDiscoverItems = ServiceDiscoveryManager.getInstanceFor(paramConnection).discoverItems(str1);
              Iterator localIterator = localDiscoverItems.getItems();
              while (localIterator.hasNext())
              {
                DiscoverItems.Item localItem = (DiscoverItems.Item)localIterator.next();
                localDiscoverInfo = ServiceDiscoveryManager.getInstanceFor(paramConnection).discoverInfo(localItem.getEntityID(), localItem.getNode());
                if (localDiscoverInfo.containsFeature("http://jabber.org/protocol/address"))
                {
                  str2 = str1;
                  break;
                }
              }
            }
            services.put(str1, str2 == null ? "" : str2);
          }
          catch (XMPPException localXMPPException)
          {
            localXMPPException.printStackTrace();
          }
      }
    return "".equals(str2) ? null : str2;
  }

  private static class PacketCopy extends Packet
  {
    private String text;

    public PacketCopy(String paramString)
    {
      this.text = paramString;
    }

    public String toXML()
    {
      return this.text;
    }
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     org.jivesoftware.smackx.MultipleRecipientManager
 * JD-Core Version:    0.6.1
 */